

package com.ra.weixin;

import java.io.File;
import java.io.Serializable;
import java.util.Date;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

import org.hibernate.annotations.Index;

import com.espirit.eap.ICheckModelDuplicateInfo;
import com.espirit.eap.sa.LoginUser;
import com.googlecode.cswish.annotation.cm;
import com.ra.landlord.TenantUser;
import com.ra.system.Register;
import com.ra.util.Address;

/**
 * 微信用户 + 普通租客
 * 
 * @author colin
 * @date 2014年10月11日
 */
@Entity
@Table(name = "ra_weixinuser")
@cm("微信用户")
public class WeixinUser implements Serializable, Register, ICheckModelDuplicateInfo {
	
	private final static String[] UNIQUE_PROPERTIES = new String[] {"phone"};
	
	@cm("楼房扫码")
	public final static Integer SOURCE_FROM_BUILDING = 11;
	@cm("合作商户")
	public final static Integer SOURCE_FROM_PARTNER = 12;
	@cm("便民服务")
	public final static Integer SOURCE_FROM_CUSTOMER = 13;
	@cm("房东")
	public final static Integer SOURCE_FROM_LANDLORD = 14;
	
	@cm("定阅")
	public final static Integer SUBSCRIBE_OK = 1;
	
	@cm("取消定阅")
	public final static Integer SUBSCRIBE_CANCEL = 0;
	
	private static final long serialVersionUID = 1235345345456L;

	private Integer id;
	
	private String openid;
	
	//1:表示订阅公众号；0：表示取消订阅
	private Integer subscribe;
	
	//微信用户昵称
	private String nickname;
	
	//登录用户名称
	private String name;
	
	// 支付宝帐号
	private String aliPay;
	
	//微信号
	private String wechat;
	
	//身份证
	private String idCard;
	
	public String getName() {
		return name;
	}

	public void setName(String loginUserName) {
		this.name = loginUserName;
	}

	//值为1时是男性，值为2时是女性，值为0时是未知
	private Integer sex;
	
	private String country;
	
	private String province;
	
	private String city;
	
	private String phone;
	
	// transient field: store the input password of APP
	private String password;
	
	private Address detailAddress;
	
	private String address;

	//用户的语言，简体中文为zh_CN
	private String language;
	
	/**
	 * 用户头像，最后一个数值代表正方形头像大小
	 * （有0、46、64、96、132数值可选，0代表640*640正方形头像），
	 * 用户没有头像时该项为空 
	 */
	private String headimgurl;
	
	//用户关注时间，为时间戳。如果用户曾多次关注，则取最后关注时间
	private Long subscribe_time;
	
	private String unionid;
	
	// null - 房东添加, 非空值为 type:id, 表示来自事件: WeixinService。xxx_SUBSCRIBE_KEY_xxx:id
	private Integer sourceFrom;
	
	// 来源参数
	private String sourceChannel;
	
	// 对应的当前租客用户, 可为空 
	private TenantUser tenantUser;
	
	// 用户注册时间(注册到本系统)
	private Date register;
	
	private LoginUser loginUser;
	
	//头像文件名
	private String photoFileName;
	
	//@Transient
	private String photoContentType;
	
	//@Transient
	private File photo;
	
	@Id
	@GeneratedValue
	@Column(name = "wx_user_id")
	@Index(name="idx_weixinuser_id")
	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	@Column(name = "openid", unique=true)
	@Index(name="idx_ra_weixinuser_openid")
	public String getOpenid() {
		return openid;
	}

	public void setOpenid(String openid) {
		this.openid = openid;
	}

	@Column(name = "subscribe")
	public Integer getSubscribe() {
		return subscribe;
	}

	public void setSubscribe(Integer subscribe) {
		this.subscribe = subscribe;
	}

	@Column(name = "nickname")
	public String getNickname() {
		return nickname;
	}

	public void setNickname(String nickname) {
		this.nickname = nickname;
	}

	@Column(name = "sex")
	public Integer getSex() {
		return sex;
	}

	public void setSex(Integer sex) {
		this.sex = sex;
	}

	@Column(name = "country")
	public String getCountry() {
		return country;
	}

	public void setCountry(String country) {
		this.country = country;
	}

	@Column(name = "province")
	public String getProvince() {
		return province;
	}

	public void setProvince(String province) {
		this.province = province;
	}

	@Column(name = "city")
	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	@Column(name = "language")
	public String getLanguage() {
		return language;
	}

	public void setLanguage(String language) {
		this.language = language;
	}

	@Column(name = "head_img_url")
	public String getHeadimgurl() {
		return headimgurl;
	}

	public void setHeadimgurl(String headimgurl) {
		this.headimgurl = headimgurl;
	}

	@Column(name = "subscribe_time")
	public Long getSubscribe_time() {
		return subscribe_time;
	}

	public void setSubscribe_time(Long subscribe_time) {
		this.subscribe_time = subscribe_time;
	}

	@Column(name = "unionid")
	public String getUnionid() {
		return unionid;
	}

	public void setUnionid(String unionid) {
		this.unionid = unionid;
	}
	
	@ManyToOne(cascade=CascadeType.MERGE)
	public LoginUser getLoginUser() {
		return loginUser;
	}

	public void setLoginUser(LoginUser loginUser) {
		this.loginUser = loginUser;
	}
	
	@ManyToOne(targetEntity=Address.class, fetch=FetchType.LAZY, cascade=CascadeType.MERGE, optional=true)
	@JoinColumn(name = "address_area_id")
	public Address getDetailAddress() {
		return detailAddress;
	}

	public void setDetailAddress(Address address) {
		this.detailAddress = address;
	}
	
	@Column(unique=true)
	@Index(name="idx_ra_weixinuser_phone")
	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}
	
	@Column(name="detailAddress")
	public String getAddress() {
		return address;
	}

	public void setAddress(String detailAddress) {
		this.address = detailAddress;
	}

	public Integer getSourceFrom() {
		return sourceFrom;
	}

	public void setSourceFrom(Integer sourceFrom) {
		this.sourceFrom = sourceFrom;
	}

	@Column(name = "sourceParameter")
	public String getSourceChannel() {
		return sourceChannel;
	}

	public void setSourceChannel(String sourceParameter) {
		this.sourceChannel = sourceParameter;
	}

	@Temporal(TemporalType.TIMESTAMP)
	public Date getRegister() {
		return register;
	}

	public void setRegister(Date register) {
		this.register = register;
	}
	
	@ManyToOne
	@JoinColumn(name = "user_id")
	public TenantUser getTenantUser() {
		return tenantUser;
	}

	public void setTenantUser(TenantUser tenantUser) {
		this.tenantUser = tenantUser;
	}
	
	@Transient
	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	@Override
	public String[] gatherProperties() {
		return UNIQUE_PROPERTIES;
	}
	
	public String getAliPay() {
		return aliPay;
	}

	public void setAliPay(String aliPay) {
		this.aliPay = aliPay;
	}

	public String getWechat() {
		return wechat;
	}

	public void setWechat(String wechat) {
		this.wechat = wechat;
	}

	public String getIdCard() {
		return idCard;
	}

	public void setIdCard(String idCard) {
		this.idCard = idCard;
	}
	
	@Transient
	public String getPhotoContentType() {
		return photoContentType;
	}

	public void setPhotoContentType(String photoContentType) {
		this.photoContentType = photoContentType;
	}

	@Transient
	public File getPhoto() {
		return photo;
	}

	public void setPhoto(File photo) {
		this.photo = photo;
	}
	
	public String getPhotoFileName() {
		return photoFileName;
	}

	public void setPhotoFileName(String photoFileName) {
		this.photoFileName = photoFileName;
	}
}
